﻿using System.Linq;
using System;
using System.Web;
using LINQExtensions;

namespace EasyLift.TMS.Web.Models
{
    public class SubcontractorRepository
    {
        private CoreBizDBEntities db = new CoreBizDBEntities();

        //Query Methods
        public IQueryable<Subcontractor> GetAllData()
        {
            return from Subcontractor in db.Subcontractors
                   orderby Subcontractor.CompanyCode descending
                   select Subcontractor;
        }

        public IQueryable<Subcontractor> GetAllDataByCompanyName(string CompanyName)
        {
            return from Subcontractor in db.Subcontractors
                   where Subcontractor.CompanyName == CompanyName
                   orderby Subcontractor.CompanyCode descending
                   select Subcontractor;
        }

        public IQueryable<Subcontractor> GetDataByName(string searchField, string searchOper, string searchString)
        {
            var ps = db.Subcontractors.Where(p => p.CompanyName.Contains(searchString));
            if (searchField == "CompanyCode")
                ps = db.Subcontractors.Where(p => p.CompanyCode.Contains(searchString));
            if (searchField == "CompanyName")
                ps = db.Subcontractors.Where(p => p.CompanyName.Contains(searchString));
            if (searchField == "All")
                ps = db.Subcontractors.Where(p => p.CompanyCode.Contains(searchString)
                                        || p.CompanyName.Contains(searchString) 
                                        || p.Remarks.Contains(searchString));

            return ps.AsQueryable();

        }

        public IQueryable<Subcontractor> GetAllData(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByName(searchField, searchOper, searchString).OrderByDescending(sortColumn);
                    else
                        return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
                else
                    return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllData().OrderByDescending(sortColumn);
                    else
                        return GetAllData().OrderBy(sortColumn);
                else
                    return GetAllData();
            }
        }




        public Subcontractor GetByID(string id)
        {
            return db.Subcontractors.SingleOrDefault(Subcontractor => Subcontractor.CompanyCode == id);
        }

        //Inset
        public void Add(Subcontractor Subcontractor)
        {
            //if (Subcontractor.StatusID == 0 )
            //    Subcontractor.StatusID = 1;
            Subcontractor.InsertedUser = HttpContext.Current.User.Identity.Name;
            Subcontractor.InsertedDate = DateTime.Now;
            Subcontractor.UpdatedUser = HttpContext.Current.User.Identity.Name;
            Subcontractor.UpdatedDate = DateTime.Now;
            Subcontractor.DataVersion = 1;

            db.Subcontractors.AddObject(Subcontractor);
        }

        //Update
        public void Update(Subcontractor Subcontractor)
        {

            Subcontractor originalSubcontractor = GetByID(Subcontractor.CompanyCode);
            
            Subcontractor.InsertedUser = originalSubcontractor.InsertedUser;
            Subcontractor.InsertedDate = originalSubcontractor.InsertedDate;
            Subcontractor.UpdatedUser = HttpContext.Current.User.Identity.Name;
            Subcontractor.UpdatedDate = DateTime.Now;
            Subcontractor.DataVersion = ++originalSubcontractor.DataVersion;
  
            db.Subcontractors.ApplyCurrentValues(Subcontractor);
        }

        //Delete
        public void Delete(Subcontractor Subcontractor)
        {
            db.Subcontractors.DeleteObject(Subcontractor);
        }

        //Persistance
        public void Save()
        {
            db.SaveChanges();
        }
    }
}